class Solution {
public:
    void sortColors(vector<int>& nums) {
        int cur = 0, left = -1, right = nums.size();
        while (cur < right)
        {
            if (nums[cur] == 0)
                swap(nums[++left], nums[cur++]);
            else if (nums[cur] == 1)
                cur++;
            else if (nums[cur] == 2)
                swap(nums[--right], nums[cur]);
            else
                assert(false);
        }
    }
};